<?php
	/**
	 *
	 * Function that extracts the text between the tags specified in $trid
	 * @param $parser the parser of MediaWiki
	 * @param $trid the transclusion id (mandatory)
	 * @return a wikitext or [TRANSCLUSION_ERROR], otherwise
	 */
	function parserFunctionTrinclude( $parser, $articleName, $trid ) {
		$rawArticle = loadRawArticle( $articleName, $trid ); 

		if ( $rawArticle != false ){	// No error
			$processedText = processArticle( $rawArticle , $trid ); 

			// The article information is loaded
			$objetoArray = TransclusionDAO::singleton() -> getTransclusion( $articleName, $trid ); 

			$oTrid = $objetoArray['trid'];
			$oSource = $objetoArray['sourceurl'];

			$resultadoFinal = $parser->recursiveTagParse ( $processedText );

			// Se agrega el texto RAW a la Base de Datos
			saveRawText( $trid , $processedText );

			// TODO: Check if it's better the span with or without color
			//$resultadoFinal = "<span style='background-color:#FFFFAA;' onclick='openSource(\"$oTrid\" , \"$oSource\"); '>$resultadoFinal</span>";
			$resultadoFinal = "<span>$resultadoFinal</span>";

			return $parser->insertStripItem( $resultadoFinal, $parser->mStripState );
		}
		else{
			return "['''TRANSCLUSION ERROR''': id $trid from article $articleName does not exists in the database]";
		}
	}

// TODO: Check or delete this function (is DOM necessary?)
//	function crearDOM ( $rawArticle , $trid ){
//		//parsing begins here:
//		$doc = new DOMDocument();
//		@$doc -> loadHTML( $rawArticle );
//		$nodes = $doc -> getElementsByTagName( 'transclusionid' . $trid );
//
//		//display what you need:
//		echo print_r($nodes->item(0));//->item(0)->nodeValue;
//	}

	/**
	 *
	 * Function that saves the raw text of an article
	 * @param $trid the id of the transclusion
	 * @param $text the text of the transclusion
	 */
	function saveRawText( $trid , $text ){
		TransclusionDAO::singleton() -> updateTranscludedText ( $trid , $text );
	}

	/**
	 *
	 * Function that loads a raw article from the database
	 * @param $trid the id of the transclusion
	 * @return a text with the raw content or
	 * 		false, otherwise
	 */
	function loadRawArticle( $articleName, $trid ){
		$array = TransclusionDAO::singleton() -> getTransclusion( $articleName, $trid );

		if ( $array != false ){ // if exists
			$wgTitle = Title::newFromURL( $array[ 'sourceurl' ] );
			if ($wgTitle == NULL) {
				unset( $wgTitle );
			}
			$wgArticle = new Article($wgTitle);
			$wgArticle -> loadContent();

			return $wgArticle -> mContent;
		}
		else{
			// Otherwise, it is cancelled
			return false;
		}
	}

	function processArticle( $textoRaw , $trid ) {
		// TODO: Mejor con DOM. REVISAR!!!!

		// The string is trimmed
		$textoConEtiqueta = stristr( $textoRaw , '<transclusionid-' . $trid);
		$textoConEtiqueta = stristr( $textoConEtiqueta , '</transclusionid-' . $trid . '>' , true );

		// The content is extracted
		$contenido = stristr ( $textoConEtiqueta , '>');

		// The spare '>' is removed
		$contenido = substr ( $contenido , 1 );

		// The text is 'cleaned' TODO: Check the Method
		cleanText($contenido);

		return trim($contenido);
	}

	function cleanText(&$texto){
		// TODO: Check and finish the cleanText function
	}

	function tagFunctionTrinclude( $input, $args, $parser, $frame ) {
		return  $parser->recursiveTagParse( htmlspecialchars( $input ) , $frame );
/* TODO: Check and delete this comment block
//		global $wgOut;
//		global $wgScriptPath;
//
//		$resultado = "";
//
//		$wgOut -> addScript( '<script src="' . $wgScriptPath . '/extensions/MediaWikiExtension/js/jquery-1.4.4.js" type="text/javascript"></script>' ); 		// Jquery
//		$wgOut -> addScript( '<script src="' . $wgScriptPath . '/extensions/MediaWikiExtension/js/mwExtension.js" type="text/javascript"></script>' );			// El archivo de la extensi�n
//		$wgOut -> addScript( '<script src="' . $wgScriptPath . '/extensions/MediaWikiExtension/js/processing-1.0.0.js" type="text/javascript"></script>' );		// Processing
//
//		if( $input != null ){
//			$parametroPaginaOrigen = $args[ "source" ];
//			$parametroTransclusionID =  $args[ "trid" ];
//
//			if( $parametroPaginaOrigen  != null ){
//				// Se sacan los valores del titulo (URL completa y solo el nombre de la p�gina)
//				$titulo = $parser -> getTitle();
//				$nombrePagina = $titulo -> getSubpageText();
//
//				if( $nombrePagina != $parametroPaginaOrigen ){
//					$resultado = "<span style='background-color:#DDDDDD;' onclick='viewSource(\"$parametroPaginaOrigen\" , \"$parametroTransclusionID\" ); '>$input</span>";
//				}
//				else{
//					$resultado = $input;
//				}
//			}
//			return $resultado;
//		}

 */
}
?>